package ee.maoinvest.frontend.controller;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import ee.maoinvest.GeneralConstants;
import ee.maoinvest.frontend.controller.menucontroller.MenuController;
import ee.maoinvest.frontend.controller.menucontroller.MenuControllerFactory;

/**
 * Servlet implementation class FrontController
 */
@WebServlet("/FrontController")
public class FrontController extends HttpServlet 
{
	private static final long serialVersionUID = 1L;
    private Logger logger = Logger.getLogger(FrontController.class);
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FrontController() 
    {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		request.setCharacterEncoding(GeneralConstants.ENCODING_UTF8);
		response.setContentType(GeneralConstants.CONTENT_TYPE_HTML_UTF8);
		
		ViewManager viewManager = new ViewManager();
		String view = "mainMenu";
		//String mode = request.getParameter("mode");
		ServletContext servletContext = getServletConfig().getServletContext();
		MenuControllerFactory menuControllerFactory  = new MenuControllerFactory();
		MenuController menuController = null;
		menuController = menuControllerFactory.create(request, response);
		logger.info("Menucontroller: " + menuController);
		if (menuController != null && !menuController.equals(""))
		{
			view = menuController.service(request, response);
			logger.info(view);
		}
		viewManager.navigate(view, request, response, servletContext);
	}

}
